package org.jc.subject.domain.handler.subject;

import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import org.jc.subject.common.enums.IsDeletedFlagEnum;
import org.jc.subject.common.enums.SubjectInfoTypeEnum;
import org.jc.subject.domain.entity.SubjectInfoBO;
import org.jc.subject.domain.entity.SubjectOptionBO;
import org.jc.subject.infra.basic.entity.SubjectJudge;
import org.jc.subject.infra.basic.service.SubjectJudgeService;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * 功能描述
 *
 * @author wanghongqian
 * @date 2024/06/24 23:03
 */
@Component
public class JudgeTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectJudgeService subjectJudgeService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoBO.getOptionList()),"选项不能为空");
        Preconditions.checkArgument(subjectInfoBO.getOptionList().size() == 1,"选项个数不合法");

        SubjectInfoBO.SubjectAnswerBO option = subjectInfoBO.getOptionList().get(0);
        SubjectJudge judge = new SubjectJudge();
        judge.setSubjectId(subjectInfoBO.getId());
        judge.setIsCorrect(option.getIsCorrect());
        judge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectJudgeService.insert(judge);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectJudge condition = new SubjectJudge();
        condition.setSubjectId(subjectId);
        List<SubjectJudge> judges = subjectJudgeService.queryByCondition(condition);
        Preconditions.checkArgument(!CollectionUtils.isEmpty(judges),"选项信息为空");

        SubjectOptionBO bo = new SubjectOptionBO();
        SubjectInfoBO.SubjectAnswerBO subjectAnswerBO = new SubjectInfoBO.SubjectAnswerBO();
        subjectAnswerBO.setIsCorrect(judges.get(0).getIsCorrect());
        List<SubjectInfoBO.SubjectAnswerBO> optionList = Collections.singletonList(subjectAnswerBO);
        bo.setOptionList(optionList);
        return bo;
    }
}
